package mpscheduling;

import java.util.concurrent.atomic.AtomicInteger;

/**
 *
 * @author jesseday
 */
public class Process implements Runnable, Comparable{
    
    private int pnum, arrival, burst, remaining, finish;
    private AtomicInteger time = new AtomicInteger();
    private boolean done;
    
    public Process(int pnum, int arr, int bur, AtomicInteger time){
        this.pnum = pnum;
        this.arrival = arr;
        this.burst = bur;
        this.remaining = bur;
        this.time = time;
    }

    @Override
    public void run() {
        if(!(this.remaining == 0))
            this.remaining--;
        
        if(this.remaining == 0)
            this.finish = time.get()+1;
    }
    
    public boolean isDone(){
        return (this.remaining == 0);
    }
    
    public int initialBurst(){
        return this.burst;
    }
    
    public int processNum(){
        return this.pnum;
    }
    
    public int arrivalTime(){
        return this.arrival;
    }
    
    public int remainingTime(){
        return this.remaining;
    }
    
    public int turnaroundTime(){
        return (finish-arrival);
    }
    
    public int waitTime(){
        return ((finish-arrival) - burst);
    }

    public int compareTo(Process p) {
        if(this.remaining < p.remainingTime())
            return -1;
        else if (this.remaining > p.remainingTime())
            return 1;
        else 
            return 0;
    }
    
    @Override
    public int compareTo(Object o) {
        o = (Process)o;
        return compareTo(o);
    }
    
}
